package com.lightsaber.trade.as.web.category;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.lightsaber.trade.as.entity.category.CategoryNode;
import com.lightsaber.trade.as.entity.category.CategoryNodeType;
import com.lightsaber.trade.as.entity.category.InputType;
import com.lightsaber.trade.as.entity.category.SourceType;
import com.lightsaber.trade.as.service.category.CategoryNodeService;
import com.lightsaber.trade.as.service.category.CategoryService;
import com.lightsaber.trade.as.service.param.ParamService;
import com.lightsaber.trade.core.web.BaseAction;

@Namespace("/category")
@Action(value = "categoryNode")
@Results({ @Result(name = "create-edit-category-node", location = "create-edit-category-node.jsp") })
public class CategoryNodeAction extends BaseAction {

    private static final long serialVersionUID = -6133932528005700711L;

    @Autowired
    CategoryNodeService categoryNodeService;
    @Autowired
    CategoryService categoryService;

    @Autowired
    ParamService paramService;

    private CategoryNode categoryNode;
    private String categoryId;
    private String categoryNodeId;

    private String categoryNodeType;

    private boolean ownerNodeIsLeaf;
    private String treeType;

    private Map<String, String> inputTypeMap;
    private Map<String, String> sourceTypeMap;

    @Override
    public String execute() {
        return list();
    }

    public String list() {
        categoryNode = categoryNodeService.findById(categoryNodeId);
        setCategoryNodeId(categoryNodeId);
        setCategoryId(categoryNode.getCategory().getId());
        setOwnerNodeIsLeaf(false);
        if (StringUtils.isBlank(categoryNode.getCategory().getBranchName())) {
            setTreeType("Root");
            setCategoryNodeType(CategoryNodeType.Root.name());
        } else {
            setTreeType("Branch");
            if (categoryNode.getCategory().getOwnerNode().getIsLeafNode() == 1) {
                setOwnerNodeIsLeaf(true);
                setCategoryNodeType(CategoryNodeType.Leaf.name());
            }
        }
        setInputTypeMap(InputType.list());
        setSourceTypeMap(SourceType.list());
        return "create-edit-category-node";
    }

    public String save() throws Exception {
        categoryNodeService.saveRoot(categoryNode);

        addActionMessage(getText(PROCESSED_SUCCESS));

        return list();
    }

    @Override
    public String input() throws Exception {
        return INPUT;
    }

    public String delete() throws Exception {
        categoryNodeService.deleteAllNodeExceptRoot(categoryNodeId);

        addActionMessage(getText(PROCESSED_SUCCESS));

        return list();
    }

    public CategoryNode getCategoryNode() {
        return categoryNode;
    }

    public void setCategoryNode(final CategoryNode categoryNode) {

        this.categoryNode = categoryNode;

        this.categoryNode.setInputType(InputType.Both);
        this.categoryNode.setSourceType(SourceType.ManualInput);
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(final String categoryId) {
        this.categoryId = categoryId;
    }

    public String getCategoryNodeId() {
        return categoryNodeId;
    }

    public void setCategoryNodeId(final String categoryNodeId) {
        this.categoryNodeId = categoryNodeId;
    }

    public String getCategoryNodeType() {
        return categoryNodeType;
    }

    public void setCategoryNodeType(final String categoryNodeType) {
        this.categoryNodeType = categoryNodeType;
    }

    public String getTreeType() {
        return treeType;
    }

    public void setTreeType(final String treeType) {
        this.treeType = treeType;
    }

    public Map<String, String> getInputTypeMap() {
        return inputTypeMap;
    }

    public void setInputTypeMap(final Map<String, String> inputTypeMap) {
        this.inputTypeMap = inputTypeMap;
    }

    public Map<String, String> getSourceTypeMap() {
        return sourceTypeMap;
    }

    public void setSourceTypeMap(final Map<String, String> sourceTypeMap) {
        this.sourceTypeMap = sourceTypeMap;
    }

    public ParamService getParamService() {
        return paramService;
    }

    public void setParamService(final ParamService paramService) {
        this.paramService = paramService;
    }

    public boolean isOwnerNodeIsLeaf() {
        return ownerNodeIsLeaf;
    }

    public void setOwnerNodeIsLeaf(final boolean ownerNodeIsLeaf) {
        this.ownerNodeIsLeaf = ownerNodeIsLeaf;
    }

}
